package com.spzx.user.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.utils.DateUtils;
import com.spzx.user.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserAddressMapper;
import com.spzx.user.api.domain.UserAddress;
import com.spzx.user.service.IUserAddressService;

/**
 * 用户地址Service业务层处理
 *
 * @author atguigu
 * @date 2024-09-28
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements IUserAddressService
{
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private IRegionService regionService;

    /**
     * 查询用户地址列表
     *
     * @return 用户地址
     */
    @Override
    public List<UserAddress> selectUserAddressList() {
        // 获取当前登录用户的Id
        Long userId = SecurityContextHolder.getUserId();
        // 根据用户Id查询用户地址
        List<UserAddress> userAddressList = userAddressMapper.selectList(new LambdaQueryWrapper<>(UserAddress.class)
                .eq(UserAddress::getUserId, userId));
        return userAddressList;
    }

    /**
     * 新增用户地址
     *
     * @param userAddress 用户地址
     * @return 结果
     */
    @Override
    public int insertUserAddress(UserAddress userAddress) {
        // 获取并设置当前登录用户的Id
        userAddress.setUserId(SecurityContextHolder.getUserId());
        // 创建时间
        userAddress.setCreateTime(DateUtils.getNowDate());
        // 根据地区code查询地区名称并封装成完整地址
        userAddress.setFullAddress(regionService.getNameByCode(userAddress.getProvinceCode())
                + regionService.getNameByCode(userAddress.getCityCode())
                + regionService.getNameByCode(userAddress.getDistrictCode())
                + userAddress.getAddress()
        );
        // 判断待新增的地址是是否为默认地址
        // 如果是 则把该用户Id下的其他地址修改为非默认地址
        if (userAddress.getIsDefault() == 1) {
            userAddressMapper.update(null, new LambdaUpdateWrapper<UserAddress>()
                    .eq(UserAddress::getUserId, userAddress.getUserId())
                    .set(UserAddress::getIsDefault, 0)
            );
        }
        // 插入新增的地址
        return userAddressMapper.insert(userAddress);
    }

    /**
     * 修改用户地址
     *
     * @param userAddress 用户地址
     * @return 结果
     */
    @Override
    public int updateUserAddress(UserAddress userAddress) {
        // 根据地区code查询地区名称并封装成完整地址
        userAddress.setFullAddress(regionService.getNameByCode(userAddress.getProvinceCode())
                + regionService.getNameByCode(userAddress.getCityCode())
                + regionService.getNameByCode(userAddress.getDistrictCode())
                + userAddress.getAddress()
        );
        // 修改时间
        userAddress.setUpdateTime(DateUtils.getNowDate());
        // 判断待修改的地址是是否为默认地址
        // 如果是 则把该用户Id下的其他地址修改为非默认地址
        if (userAddress.getIsDefault() == 1) {
            userAddressMapper.update(null, new LambdaUpdateWrapper<UserAddress>()
                    .eq(UserAddress::getUserId, userAddress.getUserId())
                    .ne(UserAddress::getId, userAddress.getId()) // 排除当前待修改的地址
                    .set(UserAddress::getIsDefault, 0)
            );
        }
        return userAddressMapper.updateById(userAddress);
    }

    // *********************************************后台管理系统接口***************************************************************************

    /**
     * 查询用户地址列表
     *
     * @param userAddress 用户地址
     * @return 用户地址
     */
    // @Override
    // public List<UserAddress> selectUserAddressList(UserAddress userAddress)
    // {
    //     return userAddressMapper.selectUserAddressList(userAddress);
    // }

}
